<!DOCTYPE html>
<html lang="en">
<title>门店管理-门禁设备添加</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div id='wrapper-iframe' class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">新增门禁</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal dropzone"  name="addForm" id="addForm"
                          style="border: 0;background: none">

                        <input type="hidden" id="id" name="id" value="${(access.id)!}">

                        <input type="hidden" id="workoutRoomNumHide" value="${(access.workoutRoomNum)!}">
                        <input type="hidden" id="deviceSerialIdHide" value="${(access.deviceSerialId)!}">

                        <input type="hidden" id="lastUpdateTime" name="lastUpdateTime" value="${(access.lastUpdateTime?string('yyyy-MM-dd HH:mm:ss'))!}">
                        <input type="hidden" id="deviceStatus" name="deviceStatus" value="${(access.deviceStatus)!}">

                        <div class="form-group"><label class="col-lg-2 control-label">所属门店</label>

                            <div class="col-lg-10">
                                <select class="form-control m-b chosen-select" id="storeId" name="storeId" tabindex="2">
                                    <#if stores??>
                                        <#list stores as store>
                                            <option value="${store.id!}" <#if access?? && access.storeId = store.id> selected</#if> > ${store.storeName!}</option>
                                        </#list>
                                    </#if>
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">门禁位置</label>
                            <div class="col-sm-10" id="storeLocation">

                                <div class="checkbox-inline i-checks"><label for="door"> <input type="radio" id="door" value="1" name="storeLocation" <#if access??><#else>checked</#if> <#if access?? && access.storeLocation = 1>checked</#if>  > <i></i> 大门 </label></div>
                                <div class="checkbox-inline i-checks"><label for="room"> <input type="radio" id="room" value="2" name="storeLocation" <#if access?? && access.storeLocation = 2>checked</#if>  > <i></i> 操房 </label></div>
                                <div class="checkbox-inline i-checks"><label for="self"> <input type="radio" id="self" value="3" name="storeLocation" <#if access?? && access.storeLocation = 3>checked</#if>  > <i></i> 自助 </label></div>
                                <!--<div class="checkbox-inline i-checks"><label for="bathroom"> <input type="radio" id="bathroom" value="4" name="storeLocation" <#if access?? && access.storeLocation = 4>浴室</#if>  > <i></i> 动态密码 </label></div>-->

                            </div>
                        </div>

                        <div class="form-group room-show"><label class="col-sm-2 control-label">操房编号</label>
                            <div class="col-sm-10" id="workoutRoomNum_box">
                                <input class="form-control" value="${(access.workoutRoomNum)!}" id="workoutRoomNum" name="workoutRoomNum" type="number" min="0">
                                <span style="color: red" id="workoutRoomNum_msg"></span>
                            </div>
                        </div>

                        <div class="form-group room-show"><label class="col-sm-2 control-label">操房名称</label>
                            <div class="col-sm-10">
                                <input class="form-control" value="${(access.workoutRoomName)!}"  id="workoutRoomName" name="workoutRoomName">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">密码类型</label>
                            <div class="col-sm-10" id="pwdChoose">

                                <div class="checkbox-inline i-checks"><label for="defaultYes"> <input type="radio" id="defaultYes" value="1" name="pwdType" <#if access??><#else>checked</#if> <#if access?? && access.pwdType = 1>checked</#if>  > <i></i> 动态密码 </label></div>
                                <div class="checkbox-inline i-checks"><label for="defaultNo"> <input type="radio" id="defaultNo" value="2" name="pwdType" <#if access?? && access.pwdType = 2>checked</#if> > <i></i> 固定密码 </label></div>
                            </div>
                        </div>

                        <div class="form-group form-dynamic"><label class="col-sm-2 control-label">进店密码</label>
                            <div class="col-sm-10">
                                <input class="form-control" value="${(access.superPwd)!}" id="superPwd"  name="superPwd">
                            </div>
                        </div>

                        <div class="form-group form-fixed"><label class="col-sm-2 control-label">设备序列号</label>
                            <div class="col-sm-10" id="deviceSerialId_box">
                                <input class="form-control" value="${(access.deviceSerialId)!}" id="deviceSerialId"  name="deviceSerialId">
                                <span style="color: red" id="deviceSerialId_msg"></span>
                            </div>
                        </div>


                    </form>
                </div>
                <div class="ibox-footer overflow">

                    <button type="button" id="goBackList" class="btn btn-default pull-left"><#if access?? && access.id??>返回列表<#else>取消添加</#if></button>
                    <button type="button" id="saveBtn"  class="btn btn-success pull-right" ><#if access?? && access.id??>更新<#else>确认添加</#if></button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>


    $(document).ready(function () {
        //初始化设置大门的操房名称.以解决
        if ( $("#id").val()>0 && $("#door").val() === 1 ) {
            $("#workoutRoomNum").val(0);
            $("#workoutRoomName").val('大门');
        }

        var isCheck = true;

        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });

        $("#storeLocation input").each(function (index, item) {
            if(index == 1 && item.checked == true) {
                $(".room-show").show();
                return false;
            } else {
                $(".room-show").hide();
            }
        })
        $("#pwdChoose input").each(function (index, item) {
            if(index == 0 && item.checked == true) {
                $('.form-dynamic').hide(400);
                return false;
            } else {
                $('.form-fixed').hide(400);
                return false;
            }
        });

        $('#pwdChoose input').on('ifChecked', function () {
            if ($(this).val() === '1') {
                $('.form-dynamic').hide(400);
                $('.form-fixed').show(400);
            } else {
                $('.form-dynamic').show(400);
                $('.form-fixed').hide(400);
            }
        });

        $('#storeLocation input').on('ifClicked', function () {

            if ($(this).val() === '2') {
                $('.room-show').show(400);
            } else {
                $('.room-show').hide(400);
            }
        });

        //门禁位置改变成非操房类型时移除对操房的校验的改变
        $("#door,#self").on("ifChecked",function (event) {
            isCheck = true;
            $("#saveBtn").removeAttr('disabled');
            if (this.value ==='1') {
                console.log("大门.....");
                $("#workoutRoomNum").val(0);
                $("#workoutRoomName").val('大门');
            }
        });

        //门禁位置选中操房时重新校验操房编号数据
        $("#room").on("ifChecked", function (event) {
            if (this.value ==='2') {
                $("#workoutRoomNum").val('');
                $("#workoutRoomName").val('');
            }
            if($("#workoutRoomNum").val()){
                $("#workoutRoomNum").trigger("change");
            } else {
                $("#workoutRoomNum_box").removeClass("has-error");
                $("#workoutRoomNum_msg").text('');
            }
        });


        //固定密码时移除对设备序列号校验的改变
        $("#defaultNo").on("ifChecked",function (event) {
            isCheck = true;
            $("#saveBtn").removeAttr('disabled');
        });

        //动态密码选中操房时重新校验序列号数据
        $("#defaultYes").on("ifChecked", function (event) {
            debugger;
            if($("#deviceSerialId").val()){
                $("#deviceSerialId").trigger("change");
            } else {
                $("#deviceSerialId_box").removeClass("has-error");
                $("#deviceSerialId_msg").text('');
            }
        });



        //校验设备操房编号是否已经存在
        $("#storeId, #workoutRoomNum").on("change",function() {
            if($("#workoutRoomNum").val()){
                $.post("checkWorkoutNum?storeId=" + $("#storeId").val() +"&workoutRoomNum="+$("#workoutRoomNum").val() ,function(result) {
                    result = JSON.parse(result);
                    $.each(result, function (i, data) {
                        console.log("操房编号result---> "+ data);
                    });
                    if (!result.result) {
                        if ($("#workoutRoomNumHide").val() == $("#workoutRoomNum").val()) {
                            //解决编辑时更改操房 ID 后又更改回当前DB 中的 ID 时会错误报重复的问题
                            $("#workoutRoomNum_box").removeClass("has-error");
                            $("#workoutRoomNum_msg").text('');
                            isCheck = true;
                            $("#saveBtn").removeAttr('disabled');
                        } else {
                            $("#workoutRoomNum_box").addClass("has-error");
                            $("#workoutRoomNum_msg").text(result.msg);
                            isCheck = false;
                            $("#saveBtn").attr('disabled', "true");
                        }
                    }else {
                        $("#workoutRoomNum_box").removeClass("has-error");
                        $("#workoutRoomNum_msg").text('');
                        isCheck = true;
                        $("#saveBtn").removeAttr('disabled');
                    }
                });
            }
        });

        //校验设备序列号是否已经存在
        $("#deviceSerialId").on("change",function() {
            $.post("checkSerialId?deviceSerialId=" + this.value ,function(result) {
                result = JSON.parse(result);
                $.each(result, function (i, data) {
                    console.log("序列号result---> "+ data);
                });
                if (!result.result) {
                    if ($("#deviceSerialIdHide").val() == $("#deviceSerialId").val()) {
                        $("#deviceSerialId_box").removeClass("has-error");
                        $("#deviceSerialId_msg").text('');
                        isCheck = true;
                        $("#saveBtn").removeAttr('disabled');
                    } else {
                        $("#deviceSerialId_box").addClass("has-error");
                        $("#deviceSerialId_msg").text(result.msg);
                        isCheck = false;
                        $("#saveBtn").attr('disabled', "true");
                    }
                }else {
                    $("#deviceSerialId_box").removeClass("has-error");
                    $("#deviceSerialId_msg").text('');
                    isCheck = true;
                    $("#saveBtn").removeAttr('disabled');

                }
            });
        });

        //返回列表页面方法
        $("#goBackList").click(function goBackList() {
            window.location = 'list';
        });

        //提交保存
        $("#saveBtn").click(function () {
            var param = $("#addForm").serialize();
            debugger;
            if (!isCheck) {
                $("#saveBtn").attr('disabled', "true");
                return false;
            }

            $("#saveBtn").attr('disabled', "true");
            $.ajax({
                type: 'post',
                url: 'accessDeviceSave',
                data: param,
                dataType : 'json',
                success: function (data) {
                    if (data && data.result) {
                        swal('保存成功!', '页面即将重新加载,请稍后...', 'success');
                        window.setTimeout(function () {
                            window.location = 'list'
                        }, 1000);
                    } else {
                        swal('保存失败!', data.msg, 'error');
                        $("#saveBtn").removeAttr('disabled');
                    }
                }
            });
        });
    });




</script>

</@body.page>
</html>